<?php

require_once SHMS_CORE_PATH."/shms_core_form.inc";
require_once SHMS_CORE_PATH."/shms_db_common.inc";
require_once SHMS_CORE_PATH."/shms_core_common.inc";

function shms_event_form_verify_user_access(&$form_state,$eid = NULL,$event = NULL){
  if(!isset($form_state['verification_done'])){
    if($event == NULL){
      if($eid == NULL)  return FALSE;
      $event = shms_core_db_load_from_table(SHMS_EVENT_TABLE, 'id', $eid);
    }
    if(empty($event) || !shms_validate_group_access(NULL,$event['person_in_charge_id'])){
      return FALSE;
    }
    $form_state['verification_done'] = TRUE;
  }
  return TRUE;
}

function shms_event_form_get_basic_struc(){
  return array(
    'name'  => array(
      '#type' => 'textfield',
      '#title'=> t("Name"),
      '#required'   => TRUE,
    ),
    'description' => array(
      '#type' => "textarea",
      '#title'=> t("Description"),
    ),
    'date' => array(
      '#type' => "date",
      '#title'=> t("Event Date"),
    ),
    'person_in_charge' => array(
      '#type' => "textfield",
      '#title'=> t("Person in Charge"),
      '#required'   => TRUE,
      '#autocomplete_path'  => SHMS_PERSON_PATH."/name_auto_complete_matrix_keyed",
    ),
    'default_cca_point' => array(
      '#type' => "textfield",
      '#title'=> t("Default CCA Points"),
      '#required'   => TRUE,
      '#element_validate' => array("shms_core_form_validate_number_field"),
    ),
  );
}

function  shms_core_event_validate_chair_matric_and_change_it_to_person_id(&$form_state){
  $matric_no = trim(array_pop(explode(" - ", $form_state['values']['person_in_charge'])));
  if(!shms_core_person_matric_number_is_correct_format($matric_no)){
    form_set_error("person_in_charge","Please Follow the Matric Number Format, or use the auto complete to complete");
    return;
  }
  
  // load person first
  $person_data = shms_core_db_load_from_table(SHMS_PERSON_TABLE,'matric_no', $matric_no);
  if(empty($person_data)){
    form_set_error("chair_pid","Sorry Person with matric number: ".$matric_no." doesn't exist.");
    return;
  }
  $form_state['values']['person_in_charge_id'] = $person_data['id'];
}

function shms_core_event_validate_name_exist(&$form_state){
  $qres = shms_core_db_load_from_table(SHMS_EVENT_TABLE,'name',$form_state['values']['name']);
  if(!empty($qres)){
    form_set_error('name',"CCA with the same name exists");
  }
}

function shms_event_form_person_in_group_table($eid,$mode = "view"){
  $add_part_str = l("Add Participants",SHMS_EVENT_PATH."/".$eid."/add_member");
  
  // define the person list of the group
  $person_list = shms_core_db_get_person_info_list_from_eid($eid);
  
  $res_arr['member_list'] = array(
    '#type'  => "fieldset",
    "#title"=> t("Participants (will be assigned with CCA points)"),
    "#theme" => "shms_event_form_person_in_group_table",
    "#weight"=> 10,
    '#tree'  => TRUE,
    'add_member' => array('#markup' => $add_part_str),
  );
  
  foreach($person_list as $pyid => $record){
    $key = $record['pyid'];
    
    $res_arr['member_list'][$key] = array(
      'name'  => array("#markup" => $record['full_name']),
      'matric_no' => array("#markup" => $record['matric_no']),
      'room_number' => array('#markup'  => $record['room_number']),
      'option'  =>
        array('#markup' => l("remove",SHMS_EVENT_PATH."/$eid/remove_participants/".$record['pyeid'],array('query'=>array("destination"=>current_path())))),
    );
  };
  
  return $res_arr;
}

function theme_shms_event_form_person_in_group_table($variables){
  $form = $variables['form']; 
  $header = array("Name","Matric No.","Room Number","Option");
  foreach($form as $pyid=>$record){
    if(!is_numeric($pyid))  continue;
    $rows[] = array(
      drupal_render($record['name']),
      drupal_render($record['matric_no']),
      drupal_render($record['room_number']),
      drupal_render($record['option']),
    );
  }
  $trailing_ele = drupal_render($form['add_member']);
  $res = "";
  if(!empty($rows)){
    $res .= "Member:".theme("table",array('header'=>$header,"rows"=>$rows));
  }
  return $res.$trailing_ele;
}

